<?php


class Admin_Form_Users extends Zend_Form {

   public function __construct($options = null) {
      parent::__construct($options);
      
      $view = Zend_Layout::getMvcInstance()->getView(); 
      $userModel = new Model_User();
      $userid = $options['params']['id'];
      $user = $userModel->findOneBy('id', $userid);
      $this->setName('usersForm');
      
      
      $countryModel =  new Model_Country();
      $countries = $countryModel->getCountryList();
      $clist = new Zend_Form_Element_Select('country');
      $clist->setRequired(true)
                 ->setLabel($view->translate->_('Country'))
                 ->setMultiOptions($countries)
      ; 
      
      $statesModel =  new Model_States();
      $states = $statesModel->getList('name', 'code', array('' => 'None'));
      $slist = new Zend_Form_Element_Select('state');
      $slist->setRequired(false)
                 ->setLabel($view->translate->_('State'))
                 ->setMultiOptions($states)
      ;
      
      
      $fields = array(
         'id' => new Zend_Form_Element_Text('id', array(
            'label' => $view->translate->_('Id'), 
            'required' => false,
            'value' => $user->id
         )),
         'username' => new Zend_Form_Element_Text('username', array(
            'label' => $view->translate->_('Username'), 
            'required' => true, 
            'value' => $user->username
         )),    
         'password' => new Zend_Form_Element_Text('password', array(
            'label' => $view->translate->_('Password'), 
            'required' => false, 
            'value' => ''
         )),      
         'password2' => new Zend_Form_Element_Text('password2', array(
            'label' => $view->translate->_('Retype Password'), 
            'required' => false, 
            'value' => ''
         )),
         'firstname' => new Zend_Form_Element_Text('firstname', array(
            'label' => $view->translate->_('Firstname'), 
            'required' => true, 
            'value' => $user->firstname
         )), 
         'lastname' => new Zend_Form_Element_Text('lastname', array(
            'label' => $view->translate->_('Lastname'), 
            'required' => true, 
            'value' => $user->lastname
         )),
         'email' => new Zend_Form_Element_Text('email', array(
            'label' => $view->translate->_('Email'), 
            'required' => true, 
            'value' => $user->email
         )),                      
         'country' => $clist,
         'state' => $slist,
         'city' => new Zend_Form_Element_Text('city', array(
            'label' => $view->translate->_('City'), 
            'required' => true, 
            'value' => $user->city
         )),
         'zip' => new Zend_Form_Element_Text('zip', array(
            'label' => $view->translate->_('Zip'), 
            'required' => false, 
            'value' => $user->zip
         )),
         'address' => new Zend_Form_Element_Text('address', array(
            'label' => $view->translate->_('Address'), 
            'required' => true, 
            'value' => $user->address
         )),   
         
         //'act' => $actList,
         'submit' => new Zend_Form_Element_Submit('login', array(
            'label' => $view->translate->_('Submit')
         ))
      );
     
      $token = Zend_Controller_Front::getInstance()->getRequest()->getPost('password');      
      if(strlen(trim($token))) {
         $fields['password2']->addValidator(new Zend_Validate_Identical(trim($token)))
   //                          ->addValidator(new Zend_Validate_StringLength(6, 30))
                             ->addFilter(new Zend_Filter_StringTrim());
   //                          ->isRequired();
                             
      }
      if($options['action'] == 'add') {
         $username = Zend_Controller_Front::getInstance()->getRequest()->getPost('username');
         $fields['username']->addValidator(new App_Validate_Unique('users', 'username'))
                            ->addValidator(new Zend_Validate_StringLength(6, 30))
                            ->addFilter(new Zend_Filter_StringTrim())                         
                            ->isRequired();
         
      }
      $this->addElements($fields);
      $this->setMethod('post');
      $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl(). '/admin/users/'.$options['params']['action']);

   }



}